package com.wisdom.view;

import org.json.JSONException;
import org.json.JSONObject;
import org.sipdroid.sipua.ui.Settings;

import com.bluecall.sipua.R;
import com.wisdom.tools.CCPConfig;
import com.wisdom.tools.HttpUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MakeCallbackActivity extends Activity {
	EditText editPhone;
    EditText editToPhone;
    Button btnMakeCall;
    private ProgressDialog mDialog;
    public static final String  callbackUrl=CCPConfig.Mobile_BasicUrl+"/api/mobile/callback";
    public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_callback);
		initResourceRefs();
	}
    
    public void goBack(View view){
		startActivity(new Intent(MakeCallbackActivity.this,HomeSettingActivity.class));
	}
	
	void showInitErrDialog(){
		new AlertDialog.Builder(this).setTitle(R.string.str_dialog_init_error_title)
				.setMessage(R.string.str_dialog_init_error_message)
				.setPositiveButton(R.string.dialog_btn,null).create().show();
	}
	
	public void logon(View view) throws JSONException{
				
				mDialog = new ProgressDialog(MakeCallbackActivity.this);
				mDialog.setMessage(getString(R.string.dialoglogon_message_text));
				mDialog.setCanceledOnTouchOutside(false);
				mDialog.show();
				String toPhone=editToPhone.getText().toString();
				String srcPhone=editPhone.getText().toString();
				JSONObject object=new JSONObject("{ToPhone:\""+toPhone+"\",SrcPhone:\""+srcPhone+"\",UserName:\""+srcPhone+"\"}");
				JSONObject data=HttpUtil.post(callbackUrl, object);
				mDialog.cancel();
				if(data==null){
					Toast toast = Toast.makeText(this,"未知错误", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.TOP , 0, 50);
					toast.show();
					return;
				}
				if(data.getBoolean("success")){
					Toast toast = Toast.makeText(this,"回拨成功，请注意接听来电"+data.getString("message"), Toast.LENGTH_LONG);
					toast.setGravity(Gravity.TOP , 0, 50);
					toast.show();
					//Toast.makeText(getApplicationContext(), "回拨成功，请注意接听来电"+data.getString("message"),Toast.LENGTH_SHORT).show();
				}else{
					Toast toast = Toast.makeText(this,"登陆失败："+data.getString("message"), Toast.LENGTH_LONG);
					toast.setGravity(Gravity.TOP , 0, 50);
					toast.show();
					//Toast.makeText(getApplicationContext(), "登陆失败："+data.getString("message"),Toast.LENGTH_SHORT).show();
				}
	}
	void initResourceRefs(){
		editPhone=(EditText)findViewById(R.id.editText_phone);
		editToPhone=(EditText)findViewById(R.id.editText_pwd);
		editPhone.setText(CCPConfig.Src_phone);
		btnMakeCall=(Button)findViewById(R.id.button_makecall);
	}
	
}
